const jwt = require('../utils/jwt')
const secret = 'b746aa49-e84d-4bac-b262-2f2f58faa174'

module.exports = async (req, res, next) => {
  const token = req.headers.token
  // console.log(token)
  try {
    const data = await jwt.verify(token, secret)
    // console.log(data)
    return next()
  } catch (error) {
    return res
      .status(200)
      .json({
        code: 401,
        msg: 'No permission'
      })
  }
}

// async function sign () {
//   const token = await jwt.sign({data: 'foobar'}, secret)
//   console.log(token)
// }


// async function verify () {
//   const token = await jwt.verify('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjoiZm9vYmFyIiwiaWF0IjoxNjM0MDM5OTU0fQ.nXq77eI1F73Asbw7MgzOpjRcQcBpiZjeSphcm4U5ymQ', secret)
//   console.log(token)
// }



exports.authApi = (req, res, next) => {
  if (!req.token) {
    return res.status(401).json({
      msg: 'No permission'
    })
  }
  next()
}

exports.authPage = (req, res, next) => {
  const whiteList = ['login']
  const path = req.path 
  if (req.token) {
    return next()
  } else {
    return res.redirect('/login')
  }
}

// sign()
// verify()
// const whiteList = ['login']
// module.exports = (req, res, next) => {
//   const path = req.path
//   // if (path == '/') {

//   // }
//   res.redirect('/login')
//   // console.log(path)
//   // try {
//   //   next()
//   // } catch (error) {
//   //   next(error)
//   // }
// }

